<?php

说明: 我们直接使用的文档, 有可能会导致: 任何知道文档地址的人都可以登陆调用! 那么就太可怕了!

->结合Security
1. [pom]
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>


2. [配置文件]
spring.security.user.name=admin
spring.security.user.password=123456


3. [配置类]
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * @author 小木
 * @date 2021/6/22
 * @desc swagger文档登陆拦截
 */
@Configuration
@EnableWebSecurity
public class ActuatorWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                // swagger页面需要添加登录校验
                .antMatchers("/swagger-ui.html", "/swagger-*", "/doc.html").authenticated()
                // 普通的接口不需要校验
//                .antMatchers("/**").permitAll()
                .and()
                .formLogin()
                .and()
                .csrf().disable();
    }

}
